#!/usr/bin/env python

import time

from kivy.logger import Logger
from kivy.uix.tabbedpanel import TabbedPanelHeader

from amgineirc.view.messageView import MessageView
from amgineirc.viewModel.channels.tabbedPanelHeaderViewModel import *

__all__ = ['ServerTabbedPanelHeaderView',
           'ChannelTabbedPanelHeaderView',
           'PrivmsgTabbedPanelHeaderView',
           'DccTabbedPanelHeaderView']

class _OpenTouchTabbedPanelHeaderView(TabbedPanelHeader, MessageView):
    def __init__(self, vm, **kargs):
        TabbedPanelHeader.__init__(self, **kargs)
        self.background_normal = 'atlas://data/images/defaulttheme/spinner' 
        self.background_down = 'atlas://data/images/defaulttheme/spinner_pressed'
        MessageView.__init__(self)
        self._view_model = vm
        self.bind(on_touch_down=self.handle_on_touch_down)
        self.register_event_type('on_touch_while_open')

    def on_touch_while_open(self, *largs):
        self._view_model.handle_on_touch_open(*largs)
    
    def handle_on_touch_down(self, stuff, touch):
        if self.collide_point(*touch.pos):
            if self.parent.tabbed_panel.current_tab == self:
                # touch while open
                self.dispatch('on_touch_while_open')

class ServerTabbedPanelHeaderView(_OpenTouchTabbedPanelHeaderView):

    def __init__(self, panel, server, **kargs):
        vm = ServerTabbedPanelHeaderViewModel(self, panel)
        _OpenTouchTabbedPanelHeaderView.__init__(self, vm, **kargs)
        self._panel = panel
        self._server = server
        self._target = server


class ChannelTabbedPanelHeaderView(_OpenTouchTabbedPanelHeaderView):
    
    def __init__(self, panel, server, target, **kargs):
        vm = ChannelTabbedPanelHeaderViewModel(self, panel)
        _OpenTouchTabbedPanelHeaderView.__init__(self, vm, **kargs)
        self._panel = panel
        self._server = server
        self._target = target

class PrivmsgTabbedPanelHeaderView(_OpenTouchTabbedPanelHeaderView):

    def __init__(self, panel, server, nick, **kargs):
        vm = PrivmsgTabbedPanelHeaderViewModel(self, panel)
        _OpenTouchTabbedPanelHeaderView.__init__(self, vm, **kargs)
        self._panel = panel
        self._server = server
        self._target = nick

class DccTabbedPanelHeaderView(_OpenTouchTabbedPanelHeaderView):

    def __init__(self, panel, peer_ip, peer_nick, **kargs):
        vm = DccTabbedPanelHeaderViewModel(self, panel)
        _OpenTouchTabbedPanelHeaderView.__init__(self, vm, **kargs)
        self._panel = panel
        self._server = peer_ip
        self._target = peer_nick
